var APP = function() {
}

APP.prototype.goHome = function() {
	$.get('../static/html/home.html', function(data) {
		$('#main-container').html(data);
	});
}

APP.prototype.reg = function() {
	$.get('../static/html/sign-up.html', function(data) {
		$('#main-container').html(data);
		$('.sign-up-header-button').addClass('active');
		$('.sign-in-header-button').removeClass('active');
		$('.sign-out-header-button').removeClass('active');
		$('.about-header-button').removeClass('active');
	});
}

APP.prototype.login = function() {
	$.get('../static/html/sign-in.html', function(data) {
		$('#main-container').html(data);
		$('.sign-in-header-button').addClass('active');
		$('.sign-up-header-button').removeClass('active');
		$('.sign-out-header-button').removeClass('active');
		$('.about-header-button').removeClass('active');
	});
}

APP.prototype.submitLogout = function() {
	$.ajax({
		url : '/s/?action=logout',
		success : function(resp) {
			if (resp.success === true) {
				// window.location.reload();
			}
		}
	});
}

APP.prototype.auth = function() {
	this.goHome();
	if ($.cookie('uid')) {
		$.ajax({
			url : '/s/?action=auth',
			success : function(resp) {
				if (resp.success === true) {
					$('.sign-up-header-button').remove();
					$('.sign-in-header-button').addClass('sign-out-header-button');
					$('.sign-out-header-button').removeClass('sign-in-header-button');
					$('.sign-out-header-button').attr('onclick', 'app.submitLogout()');
					$('.sign-out-header-button').html('<span>Logout</span>');
					$('.inbox-header-button').show();
					$('.outbox-header-button').show();
					if (resp.inbox != null) {
						(new MessageHandler()).onMessage(resp.inbox, true);
					}
				}
			}
		});
	} else
		this.login();
}

APP.prototype.nav = function() {
	var href = window.location.hash.substr(1, this.lenght);
	if (href == 'home')
		this.goHome();
	else if (href == 'sign_up')
		this.reg();
	else if (href == 'sign_in')
		this.login();
	else if (href == 'inbox')
		(new ContentLoader()).loadInbox();
	else if (href == 'outbox')
		(new ContentLoader()).loadOutbox();
	else if (typeof loader != 'undefined')
		loader.viewCategory(window.location.hash.substr(1, this.lenght));
}